<template>
    <div class="app-container">

        <!-- 搜索表单 -->
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">            
            <el-form-item label="路由名称:" prop="name">
                <el-input
                v-model="queryParams.host"
                placeholder="请输入路由字符串"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="Host:" prop="hosts">
                <el-input 
                v-model="queryParams.name"
                placeholder="请输入Host字符串"
                clearable
                size="small"
                @key.enter.native="handleQuery"
                ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        
        <!-- 表工具栏 新增删除等 -->
        <el-row :gutter="20" class="mb8">
            <el-col :span="1.5">
                <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['kong:routes:add']"                
                >新增</el-button>
            </el-col>

            <el-col :span="1.5">
                    <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    :loading="exportLoading"
                    @click="handleExport"
                    v-hasPermi="['kong:routes:export']"
                >导出</el-button>
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList()"></right-toolbar>

        </el-row>

        <!-- 路由表格展示 -->
         <el-table  v-loading="loading" :data="routesList" >
            <el-table-column label="路由名称" align="center" prop="name"></el-table-column>
            <el-table-column label="绑定服务" align="center" prop="kongService.name"></el-table-column>
            <el-table-column label="Host路由" align="center" prop="hosts"></el-table-column>
            <el-table-column label="Path路由" align="center">
                <template slot-scope="scope">
                    <span v-for="path in scope.row.paths" :key="path">
                        {{path}}  
                    </span>                    
                </template>
            </el-table-column>
            <el-table-column label="绑定插件" align="center">
                <template slot-scope="scope">
                    <el-popover v-for="plugin in scope.row.kongPlugins.data" trigger="click" placement="top" :key="plugin.name">                        
                        <el-descriptions  class="margin-top" :column="3"  border>
                            <el-descriptions-item>
                                <template slot="label">
                                    <i class="el-icon-s-open"></i>
                                    插件名:
                                </template>
                                {{plugin.name}}
                            </el-descriptions-item>

                            <el-descriptions-item>
                                <template slot="label">
                                    <i class="el-icon-view"></i>
                                    是否启用:
                                </template>
                                {{plugin.enabled}}
                            </el-descriptions-item>

                            <el-descriptions-item>
                                <template slot="label">
                                    <i class="el-icon-news"></i>
                                    创建时间:
                                </template>
                                {{dateFormatLong(plugin.created_at)}}
                            </el-descriptions-item>
                        </el-descriptions>
                        <div slot="reference" class="name-wrapper">
                            <el-button type="text" size="medium">{{plugin.name}}</el-button>
                        </div>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column label="id" align="center" prop="id"></el-table-column>
            <el-table-column label="标签信息" align="center" prop="tags"></el-table-column>
            <el-table-column label="创建时间" align="center" prop="created_at" :formatter="dateFormat"></el-table-column>
            
            <el-table-column  label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['kong:routes:edit']"
                    >编辑</el-button>

                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['kong:routes:remove']"
                    >删除</el-button>

                </template>
            </el-table-column>

        </el-table>
        
        <!-- 分页 -->
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
            />

            
        <!--新增 弹出层  -->
        <el-dialog :title="title" :visible.sync="addOpen" width="900px" append-to-body v-if="addOpen">
            <add-route @callback="handleCloseDiglog"/>
        </el-dialog>

        <el-dialog :visible.sync="editOpen" width="900px" append-to-body v-if="editOpen" @close="handleEditRouteDialogColse">
            <edit-route :routeEntity="routeEntity" @callback="handleCloseDiglog"/>
        </el-dialog>
    </div>
</template>
<script>
import moment from 'moment'
import { kongRoutesList , kongRouteDel} from "@/api/kong/routes";
import AddRoute  from "./addRoute"
import EditRoute from './editRoute';
export default {
    components: { AddRoute,EditRoute },
    props:{

    },
    data(){
        return{
            // 搜索显示标识
            showSearch: true,
            loading: false,
            routesList: [],
            // 查询条件
            queryParams:{
                pageNum: 1,
                pageSize: 10,
                //主机
                host:null

            },
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 导出遮罩层
            exportLoading: false,
            // 总数据行数
            total: 0,
            // 是否显示 新增 弹出层
            addOpen: false,
            // 是否显示 编辑 弹出层
            editOpen: false,
            // 弹出层 抬头
            title: "",

            pluginInfoVisible: false,

            // 路由实体
            routeEntity: null,
        }
    },
    created(){
        this.getList();
    },
    methods:{
        
        // 获取服务列表
        getList(){
            kongRoutesList(this.queryParams).then(res=>{
                // console.log(JSON.stringify(res));
                if(!res.rows){
                    return;
                }
                this.routesList=res.rows;
                this.total=res.total
            }).catch(err =>{

            });
        },
        
        handleAdd(){
            this.addOpen=true;
            this.title = "新增路由信息";
        },
        handleUpdate(row){
            this.routeEntity=row;
            this.editOpen=true;
        },
        handleDelete(row){            
            let routeId=row.id;
            // console.log(serviceId);          
            this.$confirm('是否确认删除服务编号为："' + routeId + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                return  kongRouteDel(routeId);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(() => {});  
        },
        handleExport(){},
        resetQuery(){},
        handleQuery(){},
        handleCloseDiglog(payload){
            this.addOpen=false;
            this.editOpen=false;
            this.msgSuccess(payload);
            // 刷新列表
            this.getList();
        },
        headleOpenPluginDetail(payload){
            this.msgSuccess(JSON.stringify(payload))
        },
        //时间处理函数
        dateFormat(row, column){
            const _date = row[column.property]*1000; 
            if(_date ==undefined){return ''};
            return moment(_date).format("YYYY-MM-DD HH:mm:ss");
        },
        dateFormatLong(_date){
            if(_date ==undefined){return ''};
            return moment(_date*1000).format("YYYY-MM-DD HH:mm:ss");
        },
        handleEditRouteDialogColse(){
            // console.log("edit route clost")
            this.getList();
        }

    }
}
</script>